package edu.utsa.cs.contactmanager.views;

import java.io.File;

import edu.utsa.cs.contactmanager.Contact;
import edu.utsa.cs.contactmanager.ContactManager;
import edu.utsa.cs.contactmanager.FileDialog;
import edu.utsa.cs.contactmanager.OnTaskCompleted;
import edu.utsa.cs.contactmanager.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.Drawable.Callback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

public class View_EditSingleContact extends Activity implements OnTaskCompleted{
	
	private Contact contact;
	
	ImageButton image;
	EditText nameEdit;
	EditText lastNameEdit;
    EditText phoneEdit;
    EditText emailEdit;
    CheckBox blacklist;
    Button save;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_edit_single_contact);
        Intent i = getIntent();
        contact = (Contact)i.getSerializableExtra("contact");
        
        
	}
	
	@Override
    public void onResume(){
    	ContactManager.open();
    	setFields();
    	super.onResume();
    };
    
    @Override
    public void onPause(){
    	ContactManager.close();
    	super.onPause();
    }
	
	private void setFields(){
		//Get the contact here!
		final OnTaskCompleted callback = this;
		
		image = (ImageButton)findViewById(R.id.imageButton);
		if(contact.getPicture()!=null && !contact.getPicture().equals("")){
			BitmapFactory.Options ops = new BitmapFactory.Options();//Saves a ton of memory, decreases image quality though.
			ops.inSampleSize = 16;
			ops.inPreferredConfig = Config.RGB_565;
			Bitmap bmp = BitmapFactory.decodeFile(contact.getPicture(), ops);
			image.setImageBitmap(bmp);
		}
		
		final Activity context = this;	
		
		image.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				File mPath = new File(Environment.getExternalStorageDirectory() + "//DIR//");
		        FileDialog fileDialog = new FileDialog(context, mPath);
		        fileDialog.setFileEndsWith(".jpg");
		        fileDialog.addFileListener(new FileDialog.FileSelectedListener() {
		            public void fileSelected(File file) {
		                Log.d(getClass().getName(), "selected file " + file.toString());

		            	
		            	Context context = getApplicationContext();
		            	CharSequence text = "Loaded picture from: " + file.toString();
		            	int duration = Toast.LENGTH_LONG;

		            	Toast toast = Toast.makeText(context, text, duration);
		            	toast.show();
		            	
		            	contact.setPicture(file.toString());
		            	callback.onTaskComplete();
		            }
		        });
		        fileDialog.showDialog();
		        callback.onTaskComplete();//Reset the view
			}
		});
        
        nameEdit = (EditText)findViewById(R.id.nameField);
        nameEdit.setText(contact.getFirstName());
        
        lastNameEdit = (EditText)findViewById(R.id.lastNameField);
        lastNameEdit.setText(contact.getLastName());
        
        emailEdit = (EditText)findViewById(R.id.emailField);
        emailEdit.setText(contact.getEmail());
        
        blacklist = (CheckBox)findViewById(R.id.blacklistCheck);
        blacklist.setChecked(contact.isBlackListed());
        
        save = (Button)findViewById(R.id.saveButton);
        save.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                save();
            }
        });
        
        final LinearLayout phoneNumbers = (LinearLayout) findViewById(R.id.phoneNumbers);
        
        phoneNumbers.removeAllViews();
        
        for(final String num : contact.getPhoneNums()){
        	
        	final EditText newNum = new EditText(this);
        	newNum.setText(num);
        	
        	final Button removeButton = new Button(this);
        	removeButton.setText("Remove");
        	removeButton.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					phoneNumbers.removeView(newNum);
					phoneNumbers.removeView(removeButton);
					contact.removePhoneNum(num);
					callback.onTaskComplete();
				}
			});
        	LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        	LinearLayout line = new LinearLayout(this);
        	line.setOrientation(LinearLayout.HORIZONTAL);
        	line.addView(newNum, par);
        	line.addView(removeButton, par);
        	
        	phoneNumbers.addView(line);
        	
        }
        
        
        //New phone number implementation here
        phoneEdit = (EditText)findViewById(R.id.phoneField);
        
	}

	private void save(){
		contact.setFirstName(nameEdit.getText().toString());
		contact.setLastName(lastNameEdit.getText().toString());
        contact.setEmail(emailEdit.getText().toString());
        contact.setBlackListed(blacklist.isChecked());
        
        //Phone number implementation here
        
        
        
        contact.addPhoneNum(phoneEdit.getText().toString());
        
        ContactManager.updateContact(contact);
        
        Context context = getApplicationContext();
    	CharSequence text = "Contact Saved";
    	int duration = Toast.LENGTH_LONG;

    	Toast toast = Toast.makeText(context, text, duration);
    	toast.show();

    	onBackPressed();
	}

	@Override
	public void onTaskComplete() {
		setFields();
	}

}
